package com.moore.mapper;

import com.moore.bean.Comment;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CommentMapper {

    @Select("SELECT COUNT(*) > 0 FROM comment WHERE community_id = #{communityId} AND candidate_id = #{candidateId}")
    boolean existsByCommunityIdAndCandidateId(@Param("communityId") Long communityId, @Param("candidateId") Long candidateId);

    @Select("SELECT * FROM comment WHERE community_id = #{communityId}")
    List<Comment> getCommentsByCommunityId(Long communityId);

    @Insert("INSERT INTO comment (comment_id, community_id, comment_index, comment_content, candidate_id, time, comment_like_count) VALUES (#{commentId}, #{communityId}, #{commentIndex}, #{commentContent}, #{candidateId}, now(), #{commentLikeCount})")
    int insertComment(Comment comment);

    @Delete("DELETE FROM comment WHERE comment_id = #{commentId}")
    int deleteComment(Long commentId);

    @Update("UPDATE comment SET comment_like_count = comment_like_count + 1 WHERE comment_id = #{commentId}")
    void increaseCommentLikeCount(Long commentId);

    @Update("UPDATE comment SET comment_like_count = comment_like_count - 1 WHERE comment_id = #{commentId}")
    void decreaseCommentLikeCount(Long commentId);

}